package net.studou.action;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.studou.bean.tools.BeanUtils;
import net.studou.bean.tools.Properties;
import net.studou.po.User;
import net.studou.service.UserService;
import net.studou.service.VerifyCodeService;
import net.studou.vo.VMessage;
import net.studou.vo.VUser;

import org.apache.struts2.ServletActionContext;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;

@Component("basicAction")
public class BasicAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1182513213955969723L;

	@Resource(name = "userService")
	private UserService userService;
	
	@Resource(name = "verifyCodeService")
	private VerifyCodeService verifyCodeService;

	private VUser user;

	private VMessage jsonMessage;

	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpSession getSession() {
		return ServletActionContext.getRequest().getSession();
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public void setFeedBackMesssage(VMessage message) {
		this.clearFieldErrors();
		if (message.getType() == VMessage.ERROR) {
			this.addFieldError("errorMsgTitle", message.getTitle());
			this.addFieldError("errorMsgContent", message.getContent());
		} else if (message.getType() == VMessage.SUCCESS) {
			this.addFieldError("successMsgTitle", message.getTitle());
			this.addFieldError("successMsgContent", message.getContent());
		} else if (message.getType() == VMessage.FORBID) {
			this.addFieldError("forbidMsgTitle", message.getTitle());
			this.addFieldError("forbidMsgContent", message.getContent());
		}
	}

	public void setFieldErrorMessage(String key, String message) {
		if (message != null) {
			getRequest().setAttribute(key, message);
			addFieldError("", ""); // 用来迫使struts跳转到INPUT页面
		}
	}

	public VMessage getJsonMessage() {
		return jsonMessage;
	}

	public void setJsonMessage(VMessage jsonMessage) {
		this.jsonMessage = jsonMessage;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public VerifyCodeService getVerifyCodeService() {
		return verifyCodeService;
	}

	public void setVerifyCodeService(VerifyCodeService verifyCodeService) {
		this.verifyCodeService = verifyCodeService;
	}

	public VUser getUser() {
		return user;
	}

	public void setUser(VUser user) {
		this.user = user;
	}
	
	/**
	 * 从session中获取PUser
	 * @param fields 指定需要获取的属性
	 * @return
	 * @throws Exception
	 */
	public User getUserFromSession(String fields) {
		VUser vUser = (VUser) getSession().getAttribute("userInfo");
		User pUser = new User();
		BeanUtils.copyBean(vUser, pUser, new Properties(fields));
		return pUser;
	}
	
	public String getError() {
		return "error";
	}
	
	public String getLock() {
		return "lock";
	}
	
	public String getLogin() {
		return "index";
	}
}
